from amplitude.model import Model
import users, items

class Group(Model):
    relation = 'groups'
    pk_fields = ['name']

    def members(self, update=False):
        """many-to-many relationship with members through groups_memberships"""
        if self._members is None or update==True:
            self._members = users.User(self.db).select(where="email in (select email from groups_memberships where groupname='%(name)s')" % self)
        return self._members

    def items(self, update=False):
        """many-to-many relationship with items through groups_items"""
        if self._items is None or update==True:
            self._items = items.Item(self.db).select(where="id in (select item_id from groups_items where groupname='%(name)s')" % self)
        return self._items

